import {AuthUser} from "./User";

let authUser: AuthUser = null;

/**
 * 获取登录用户信息
 */
export function getAuthUser(): AuthUser {
    if (authUser && authUser.loginFlag) {
        return authUser;
    }
    let json = window.localStorage.getItem("AuthUser");
    if (json) {
        authUser = JSON.parse(json);
        authUser.loginFlag = true;
    }
    return authUser;
}

/**
 * 判断是否有权限
 * @param buttonName 按钮名称
 */
export function permit(buttonName: String): boolean {
    if(!isLogin()){
        return false;
    }
    let buttons = getAuthUser().buttons;
    return buttons && buttons.includes(buttonName);
}

export function isLogin():boolean{
    let user = getAuthUser();
    return user && user.loginFlag;
}

